#Gopher文件访问协议
#   实现从主机上请求相关文档浏览的功能
#

import socket

def getCfg():
    file = open('./cfg', 'r')
    lines = file.readlines()
    host = lines[0].strip('host=').strip('\n')
    port = lines[1].strip('port=').strip('\n')
    filename = lines[2].strip('filename=').strip('\n')
    file.close()
    return (host, int(port), filename)

sockfd = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

print('please input remote request filename:')

cfg = getCfg()
print(cfg)

host = cfg[0]
port = cfg[1]
filename = cfg[2]

try:
    sockfd.connect((host, port))

    sockfd.sendall(filename.encode())

    while True:
        data = sockfd.recv(1024)
        if not data:
            break
        print(data.decode())

    sockfd.close()
except:
    sockfd.close()